<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller
{
	public function __construct()
	{
        parent::__construct();
		//$this->load->model(array('general_model'));
		$this->load->model('user_model');
		$this->template->set_template('template/admin_login_template');
		$this->admin=$this->session->userdata('admin');
		//print_r($this->admin);
    }
	public function index()
	{
		if($this->general_model->loggedin("admin") == FALSE)
		{
			redirect('admin/users/login');
		}
		
		$this->template->set_template('template/admin_template');
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'All Users'));
		if(adminusertype(1))
		{
			$data['users']=$this->general_model->select("users");
		}
		else
		{
			$data['users']=$this->general_model->select("users","*","groupid = 3");
		}
		$this->template->content->view('admin/user/allusers',$data);
			
		$this->template->publish();
		
	}
	public function addusers()
	{
		if($this->general_model->loggedin("admin") == FALSE)
		{
			redirect('admin/users/login');
		}
	
		$this->template->set_template('template/admin_template');
		$data['breadcrumb'] = set_crumbs(array('users' => 'All Users', current_url() => 'Add User'));
		$rules_adduser = $this->general_model->rules_adduser;
		$this->form_validation->set_rules($rules_adduser);
		if ($this->form_validation->run() == TRUE)
		{
			$userdata=array(
				"groupid"=>$this->input->post('group_id'),
				"group_type"=>$this->input->post('group_type'),
				"firstname"=>$this->input->post('firstname'),
				"lastname"=>$this->input->post('lastname'),
				"email"=>$this->input->post('email'),
				"password"=>md5($this->input->post('password')),
				"phoneno"=>$this->input->post('phoneno'),
				"address2"=>$this->input->post('address2'),
				"address2"=>$this->input->post('address1'),
				"city"=>$this->input->post('city'),
				"state_id"=>$this->input->post('state_id'),
				"country_id"=>$this->input->post('country_id'),
				"zip"=>$this->input->post('zip'),
				"activated"=>$this->input->post('enabled'),
				"activation_code"=>"trial",
				"date_added"=>date('Y-m-d H:i:s')
				
			);
			$userid=$this->general_model->insert('users',$userdata);
			$this->session->set_flashdata('message', '<p class="success">Users added Saved.</p>');
			redirect(ADMIN_PATH . "/users");
		}
        $data['groups'] = $this->general_model->select("user_group");
		$data['countries'] = $this->general_model->select("country");
		$this->template->content->view('admin/user/adduser',$data);
			
		$this->template->publish();
	}
	public function edituser()
	{
		//print_r($_POST);
		if($this->general_model->loggedin("admin") == FALSE)
		{
			redirect('admin/users/login');
		}
		$userid=$this->uri->segment(4);
		$this->template->set_template('template/admin_template');
		$data['breadcrumb'] = set_crumbs(array('users' => 'All Users', current_url() => 'Edit User'));
		$rules_adduser = $this->general_model->rules_adduser;
		$this->form_validation->set_rules($rules_adduser);
		if ($this->form_validation->run() == TRUE)
		{
			
			if ($this->input->post('password')!='')
            {
                $updatepassword=array("password"=>md5($this->input->post('password')));
				$this->general_model->update('users',$updatepassword,"userid = ".$userid);
            }
			
			$userdata=array(
				"groupid"=>$this->input->post('group_id'),
				"group_type"=>$this->input->post('group_type'),
				"firstname"=>$this->input->post('firstname'),
				"lastname"=>$this->input->post('lastname'),
				"email"=>$this->input->post('email'),
				
				"phoneno"=>$this->input->post('phoneno'),
				"address"=>$this->input->post('address'),
				"address2"=>$this->input->post('address2'),
				"city"=>$this->input->post('city'),
				"state_id"=>$this->input->post('state_id'),
				"country_id"=>$this->input->post('country_id'),
				"zip"=>$this->input->post('zip'),
				"activated"=>$this->input->post('enabled'),
				"activation_code"=>"trial",
				"date_modified"=>date('Y-m-d H:i:s')
				
			);
			$zapmemberdata=array("activated"=>$this->input->post('enabled'));
			$this->general_model->update('users',$userdata,"userid = ".$userid);
			$this->general_model->update("zap_member",$zapmemberdata,"userid = ".$userid);
		
			$this->session->set_flashdata('message', '<p class="success">Users Updated Saved.</p>');
			redirect(ADMIN_PATH . "/users");
		}
		$data['groups'] = $this->general_model->select("user_group");
		$data['countries'] = $this->general_model->select("country");
		$data['user']=$this->general_model->select_val("users","*","userid = ".$userid);
		$this->template->content->view('admin/user/edituser',$data);
			
		$this->template->publish();
	}
	public function deleteuser()
	{
		if ($this->input->post('selected'))
        {
            $selected = $this->input->post('selected');
        }
        else
        {
            $selected = (array) $this->uri->segment(5);
        }
		
		$this->general_model->delete_rows('users','userid',$selected);
		$this->general_model->delete_rows('addmate_basic','userid',$selected);
		$this->general_model->delete_rows('addmate_contact','userid',$selected);
		$this->general_model->delete_rows('addmate_kinmate','userid',$selected);
		$this->general_model->delete_rows('addmate_schoolmate','userid',$selected);
		$this->general_model->delete_rows('addmate_faithmate','userid',$selected);
		$this->general_model->delete_rows('addmate_bizmate','userid',$selected);
		$this->general_model->delete_rows('addmate_tripmate','userid',$selected);
		$this->general_model->delete_rows('addmate_polimate','userid',$selected);
		$this->general_model->delete_rows('addmate_likemate','userid',$selected);
		$this->general_model->delete_rows('addmate_heartmate','userid',$selected);
		$this->general_model->delete_rows('addmate_mailbox','userid',$selected);
		
		$this->general_model->delete_rows('addmate_profile_setting','userid',$selected);
		foreach($selected as $id)
		{
			$fileinfo=$this->general_model->select("addmate_photo","photo","userid =".$id);
			foreach($fileinfo as $photo)
			{
				unlink(FCPATH.'assets/uploads/images/addmate/'.urldecode($photo['photo']));
			}
		}
		$this->general_model->delete_rows('addmate_photo','userid',$selected);
		
		$this->session->set_flashdata('message', '<p class="success">Users deleted Successfully.</p>');
		redirect(ADMIN_PATH . "/users");
	}
	public function login()
	{
		if($this->general_model->loggedin("admin") == FALSE)
		{
			// SET VALIDATION RULES
			$rules = $this->general_model->rules;
			$this->form_validation->set_rules($rules);
			//process a form
			if ($this->form_validation->run() == TRUE)
			{
				// We can login and redirect
				$query = $this->user_model->validateadmin();
				
				if($query) // if the user's credentials validated...
				{
					redirect('admin');
				}
				
				else 
				{
					$this->session->set_flashdata('message', 'That email/password combination does not exist');
					redirect('admin/users/login', 'refresh');
				}
			}
			//$cookie_user = $this->rememberme->verifyCookie();
			//print_r($cookie_user);
			/*if ($cookie_user) {
				// find user id of cookie_user stored in application database
				$user = User::findUser($cookie_user);
				// set session if necessary
				if (!$this->session->userdata('user_id')) {
					$this->session->set_userdata('user_id', $user);
				}
				$this->user = $user;
			}
			else if ($this->session->userdata('user_id')) {
				$this->user = $this->session->userdata('user_id');
			}*/
			$this->template->content->view('admin/user/login');
			// publish the template
			$this->template->publish();
		}
		else
		{
			redirect('admin');
		}
		
	}
	public function forgot_password()
	{
		$this->template->content->view("admin/user/forgot_password");
		$this->template->publish();
	}
	public function logout()
	{
		$this->user_model->logout();
		redirect('admin');
	}
	public function groups()
	{
		$this->template->set_template('template/admin_template');
        $data['breadcrumb'] = set_crumbs(array(current_url() => 'User Groups'));
        $data['groups'] = $this->general_model->select("user_group");
        

       
		$this->template->content->view('admin/user/groups',$data);
			
		$this->template->publish();
	}
	function editgroups()
    {
        // Init
        $this->template->set_template('template/admin_template');
        $data['breadcrumb'] = set_crumbs(array('users/groups' => 'User Groups', current_url() => 'Group Edit'));
        $data['permissions'] = array();
        $group_id = $this->uri->segment(4);
        $data['permission_options'] =  unserialize(ADMIN_ACCESS_OPTIONS);
		$data['group']=$this->general_model->select_val("user_group","*","group_id = " . $group_id);
        
        
        // Validate Form
        $this->form_validation->set_rules('name', 'Group Name', "trim|required|callback_name_check[$group_id]");
        $this->form_validation->set_rules('permissions[access][]', 'Access Permissions', "trim");

        

        if ($this->form_validation->run() == TRUE)
        {
            $Group = new Groups_model();

            // Load group to update if in edit mode
            if ( ! empty($group_id))
            {
                $Group->get_by_id($group_id);
            }

            $Group->from_array($this->input->post());

            // If permissions posted serialize for db
            if ($this->input->post('permissions') && $this->input->post('type') == 'administrator')
            {
                $Group->permissions = serialize($this->input->post('permissions'));
            }
            elseif (($edit_mode && $data['Group']->modifiable_permissions) 
                ||  ! $edit_mode 
                || ($edit_mode && $Group->type == ADMINISTRATOR && $this->Group_session->type == SUPER_ADMIN)) // Don't clear permissions if group had modifiable permissions disabled
            {
                $Group->permissions = NULL;
            }

            // Save Changes
            $Group->save(); 

            $this->session->set_flashdata('message', '<p class="success">User Group Saved.</p>');

            redirect(ADMIN_PATH . '/users/groups'); 
        }

        $this->template->content->view('admin/user/editgroups',$data);
			
		$this->template->publish();
    }

	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */